﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate;
using NHibernate.Criterion;
using System.Collections;
using System.Web.UI.HtmlControls;
using ZLCMS.Business.Util;
using System.Web.Security;

namespace ZLCMS.Web.SystemManage.Login
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(UserKit.GetUserAccountWithoutException()))
            {
                Response.Redirect("~/SystemManage/");
            }
        }

        protected void lg_Authenticate(object sender, AuthenticateEventArgs e)
        {
            TextBox code = (TextBox)lg_userlogin.FindControl("tb_verifyid");

            string checkCode = CheckCodeKit.GetCheckCode();

            if (checkCode == null)
            {
                lg_userlogin.FailureText = "验证码过期，请重试。";
                return;
            }

            if (checkCode  != code.Text.Trim())
            {
                lg_userlogin.FailureText = "验证码输入错误，请重试。";
                return;
            }

            try
            {
                String md5Code = FormsAuthentication.HashPasswordForStoringInConfigFile(lg_userlogin.Password.Trim(), "MD5").ToLower();
                if (UserKit.UserLogin(lg_userlogin.UserName.Trim(), md5Code))
                {
                    Response.Redirect("~/SystemManage/");
                }
                else
                {
                    lg_userlogin.FailureText = "用户名或密码输入错误，请重试。";
                }
            }
            catch (System.Exception ex)
            {
                lg_userlogin.FailureText = ex.Message;
            }
        }

        protected void imgbt_reset_Click(object sender, ImageClickEventArgs e)
        {
            TextBox Verifyid = (TextBox)lg_userlogin.FindControl("tb_verifyid");

            lg_userlogin.UserName = "";
            Verifyid.Text = "";
        }
    }
}
